class Stock < ActiveRecord::Base
  attr_accessible :company,  :name, :purchase_cost, :purchase_date, :quantity, :sale_cost, :sold, :seller_id, :purchase_quantity,:free_quantity, :commission, :tax_percentage
  after_initialize :init
  validates :quantity, :numericality => { :only_integer => true, :greater_than => -1 }
  validates :purchase_quantity, :numericality => { :only_integer => true, :greater_than => -1 }
  validates :purchase_cost, :numericality => { :greater_than => 0 }
  validates :sale_cost, :numericality => { :greater_than => 0 }

  def init
    self.sold ||= false
    self.purchase_date ||= Date.today
  end

  def self.search(search)
      if search
       find(:all, :conditions => ['name LIKE ? OR company LIKE ? AND quantity>0', "%#{search}%","%#{search}%"])
      else
        find(:all)
      end
  end



  def update_stock_quantity(available_stock_quantit)
    self.quantity = avaliable_stock_quantity
    self.save
  end
end
